﻿Imports Autodesk.AutoCAD.DatabaseServices
Imports Licenta3.Commanding
Imports Autodesk.AutoCAD.Geometry
Imports Licenta3.Common.Classes
Imports System.Drawing.Printing
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.EditorInput

Public Class Anexa16ViewModel
    Inherits AnexaViewModel(Of Anexa16Class)

    Public Sub New()
        Anexa.Autorizat = Setari.currentAutorizat
        Anexa.OCPI = Anexa.Autorizat.DefaultOCPI
        Anexa.BCPI = Anexa.Autorizat.DefaultBCPI

        _SelectConstructieGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectConstructieGeometry, AddressOf CanSelectConstructieGeometry)
        _SelectImobilGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectImobilGeometry, AddressOf CanSelectImobilGeometry)
        _GenereazaPADCommand = New DelegateCommand(Of Object)(AddressOf GenereazaPAD, AddressOf CanGenereazaPAD)
        _SelectImobileGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectImobileGeometry, AddressOf CanSelectImobileGeometry)
        _NumberConstructiiCommand = New DelegateCommand(Of Object)(AddressOf NumberConstructii, AddressOf CanNumberConstructii)
    End Sub

    Private _Dezlipire As Boolean
    Public Property Dezlipire As Boolean
        Get
            Return _Dezlipire
        End Get
        Set(value As Boolean)
            _Dezlipire = value
            OnPropertyChanged("Dezlipire")
        End Set
    End Property
    Private _DataPAD As Date = Today
    Public Property DataPAD As Date
        Get
            Return _DataPAD
        End Get
        Set(value As Date)
            _DataPAD = value
            OnPropertyChanged("DataPAD")
        End Set
    End Property
    Public Property SelectConstructieGeometryCommand As ICommand
    Public Sub SelectConstructieGeometry(constr As Constructie)
        constr.Polyline.SelectGeometry()
        '' de implementat
    End Sub
    Public Function CanSelectConstructieGeometry()
        Return True
    End Function
    Public Property NumberConstructiiCommand As ICommand
    Public Sub NumberConstructii()
        Dim dwg As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = dwg.Editor
        Dim acCurDb = dwg.Database

        Dim opts As New PromptEntityOptions(vbLf & "Selecteaza o Constructie")
        opts.SetRejectMessage(vbLf & "Musai polilinie!")
        opts.AddAllowedClass(GetType(Polyline), True)

        Dim per As PromptEntityResult
        Using dl As DocumentLock = dwg.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True)

            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction
                '' Open the Block table for read
                Dim acBlkTbl As BlockTable
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                '' Open the Block table record Model space for write
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim i As Integer = 0
                Do
                    i += 1
                    per = ed.GetEntity(opts)
                    If per.Status = PromptStatus.OK Then
                        Dim pl As Polyline = acTrans.GetObject(per.ObjectId, OpenMode.ForWrite)
                        If pl.Closed = False Then
                            MsgBox("Atentie! Polilinia nu este inchisă și va fi inchisă acum!")
                        End If
                        FormatPolyline(pl)

                        Dim c As New DBText
                        With c
                            .SetDatabaseDefaults()
                            .HorizontalMode = TextHorizontalMode.TextMid
                            .VerticalMode = TextVerticalMode.TextBottom
                            .AlignmentPoint = centroid(pl)
                            .TextString = "C" & i.ToString
                            .Height = 3 * CDec(Anexa.Scara / 1000)
                            .AdjustAlignment(acCurDb)
                        End With
                        acBlkTblRec.AppendEntity(c)
                        acTrans.AddNewlyCreatedDBObject(c, True)

                    End If
                Loop Until per.Status = PromptStatus.Cancel
                acTrans.Commit()
            End Using

        End Using
    End Sub
    Public Function CanNumberConstructii()
        If Anexa.Scara <> Nothing Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Property SelectImobilGeometryCommand As ICommand
    Public Sub SelectImobilGeometry()
        Anexa.Imobil.Polyline.SelectGeometry()
        Anexa.Imobil.UpdateVecini()
    End Sub
    Public Function CanSelectImobilGeometry()
        Return True
    End Function

    Public Property GenereazaPADCommand As ICommand
    Public Property CategoriiFolosinta As New CategorieFolosinta
    Public Property SelectImobileGeometryCommand As ICommand
    Public Sub SelectImobileGeometry(im As Imobil)
        im.Polyline.SelectGeometry()
        'MsgBox("imobile select")
        'MsgBox(im.Geometry.NumberOfVertices.ToString)
    End Sub
    Public Function CanSelectImobileGeometry()
        Return True
    End Function
    Public Sub GenereazaPAD()
        Try
            Dim tipDoc As String = "alipire"
            If Dezlipire Then tipDoc = "dezlipire"
            Dim maxHeight, maxWidth As Integer
            Dim jos, sus As Integer
            Dim topMargin As Integer = 10
            Dim bottomMargin As Integer = 10
            Dim leftMargin As Integer = 20
            Dim rightMargin As Integer = 10
            Dim tblMargin As Integer = 10

            Dim rowHeight As Decimal = 3.5
            Dim txtRowHeight As Decimal = 2.5
            Dim A4p, A4l, A3p, A3l, paper, chenar As New Rectangle
            A4p.Height = 297
            A4p.Width = 210
            A4l.Height = 210
            A4l.Width = 297
            A3p.Height = 420
            A3p.Width = 297
            A3l.Height = 297
            A3l.Width = 420
            paper = A4p
            Dim bl, br, tr, tl, o As New Point2d
            Dim fitScale As Decimal
            Dim sc As Scari = Scari.NA
            Dim scA As Decimal
            Dim paperFound As Boolean = False
            sus = rowHeight * 8
            jos = tblMargin + rowHeight * (14 + Anexa.Imobile.Count)
            For i = 1 To 2

                maxHeight = paper.Height - topMargin - bottomMargin - sus - jos
                maxWidth = paper.Width - leftMargin - rightMargin - 2 * rowHeight ''rowHeight sunt pentru coordonate
                'MsgBox(maxHeight & " " & maxWidth)
                bl = New Point2d(Math.Floor(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MinPoint.X / 5) * 5, Math.Floor(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MinPoint.Y / 5) * 5)
                tr = New Point2d(Math.Ceiling(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MaxPoint.X / 5) * 5, Math.Ceiling(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MaxPoint.Y / 5) * 5)
                br = New Point2d(tr.X, bl.Y)
                tl = New Point2d(bl.X, tr.Y)
                fitScale = Math.Max((tr.Y - bl.Y) * 1000 / maxHeight, (tr.X - bl.X) * 1000 / maxWidth)
                For Each value As Scari In [Enum].GetValues(GetType(Scari))
                    If value > fitScale Then
                        sc = value
                        Exit For
                    End If
                Next
                If sc <> Scari.NA Then
                    Exit For
                Else
                    paper = A3p
                End If
            Next
            'MsgBox(sc.ToString)
            If sc <> Scari.NA Then
                Anexa.Scara = sc
            Else
                MsgBox("Nu incape nici pe A3 - 1:5000!")
                Exit Sub
            End If
            scA = CDec(sc / 1000)
            chenar.Width = (paper.Width - leftMargin - rightMargin)
            chenar.Height = (paper.Height - topMargin - topMargin)
            o = New Point2d(bl.X - ((paper.Width - leftMargin - rightMargin) * scA - (br.X - bl.X)) / 2, (bl.Y + tr.Y) / 2 - (maxHeight / 2 + jos) * scA)

            '' Get the current document and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database
            Using dl As DocumentLock = acDoc.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True)
                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                    '' Open the Block table for read
                    Dim acBlkTbl As BlockTable
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                    '' Open the Block table record Model space for write

                    Dim acBlkTblRec As BlockTableRecord
                    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                    Dim acLyrTbl As LayerTable
                    acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
                    Dim oldLayer As LayerTableRecord
                    oldLayer = CType(acTrans.GetObject(acCurDb.Clayer, OpenMode.ForRead), LayerTableRecord)
                    Dim AIMLayerName As String = "AIM"
                    If acLyrTbl.Has(AIMLayerName) = False Then
                        Dim acLyrTblRec As LayerTableRecord = New LayerTableRecord()
                        '' Assign the layer the ACI color 1 and a name
                        acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
                        acLyrTblRec.Name = AIMLayerName
                        '' Upgrade the Layer table for write
                        acLyrTbl.UpgradeOpen()
                        '' Append the new layer to the Layer table and the transaction
                        acLyrTbl.Add(acLyrTblRec)
                        acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
                        acCurDb.Clayer = acLyrTbl(AIMLayerName)
                    Else
                        acCurDb.Clayer = acLyrTbl(AIMLayerName)
                    End If
                    ''inserare nord
                    Try
                        Dim nordDb As New Database(False, True)
                        nordDb.ReadDwgFile(assemblyPath + "\blocks\nord.dwg", FileOpenMode.OpenForReadAndAllShare, True, "")
                        Dim nid As ObjectId = acCurDb.Insert("nord", nordDb, True)
                        Dim nordRef As New BlockReference(New Point3d(tr.X + 8, tr.Y, 0), nid)
                        nordRef.ScaleFactors = New Scale3d(scA, scA, scA)
                        acBlkTblRec.AppendEntity(nordRef)
                        acTrans.AddNewlyCreatedDBObject(nordRef, True)
                        nordDb.Dispose()
                    Catch ex As Exception
                        MsgBox(ex.Message + ex.Source)

                    End Try

                    Dim paperRect As New Polyline(4)
                    paperRect.AddVertexAt(0, New Point2d(o.X - scA * leftMargin, o.Y - scA * bottomMargin), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X + scA * (paper.Width - leftMargin), o.Y - scA * bottomMargin), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X + scA * (paper.Width - leftMargin), o.Y + scA * (paper.Height - topMargin)), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X - scA * leftMargin, o.Y + scA * (paper.Height - topMargin)), Nothing, Nothing, Nothing)
                    paperRect.Closed = True
                    acBlkTblRec.AppendEntity(paperRect)
                    acTrans.AddNewlyCreatedDBObject(paperRect, True)
                    Dim col1 As New Point3dCollection
                    For i = 0 To paperRect.NumberOfVertices - 1
                        col1.Add(paperRect.GetPoint3dAt(i))
                    Next

                    Dim chenarRect As New Polyline(4)
                    chenarRect.AddVertexAt(0, o, Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X + scA * chenar.Width, o.Y), Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X + scA * chenar.Width, o.Y + scA * chenar.Height), Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X, o.Y + scA * chenar.Height), Nothing, Nothing, Nothing)
                    chenarRect.Closed = True
                    acBlkTblRec.AppendEntity(chenarRect)
                    acTrans.AddNewlyCreatedDBObject(chenarRect, True)

                    Dim title As New DBText
                    title.SetDatabaseDefaults()
                    title.HorizontalMode = TextHorizontalMode.TextMid
                    title.VerticalMode = TextVerticalMode.TextBottom
                    title.AlignmentPoint = New Point3d(o.X + scA * chenar.Width / 2, o.Y + scA * (chenar.Height - 1.5 * rowHeight), 0)
                    title.TextString = "Plan de amplasament și delimitare"
                    title.Height = 3.5 * scA
                    title.AdjustAlignment(acCurDb)
                    acBlkTblRec.AppendEntity(title)
                    acTrans.AddNewlyCreatedDBObject(title, True)

                    Dim scara As New DBText
                    scara.SetDatabaseDefaults()
                    scara.HorizontalMode = TextHorizontalMode.TextMid
                    scara.VerticalMode = TextVerticalMode.TextBottom
                    scara.AlignmentPoint = New Point3d(title.AlignmentPoint.X, title.AlignmentPoint.Y - scA * 1.5 * rowHeight, 0)
                    scara.TextString = "Scara 1:" & CInt(sc).ToString
                    scara.Height = 3 * scA
                    scara.AdjustAlignment(acCurDb)
                    acBlkTblRec.AppendEntity(scara)
                    acTrans.AddNewlyCreatedDBObject(scara, True)

                    Dim tabel1 As New Table
                    tabel1.SetDatabaseDefaults()
                    tabel1.Position = New Point3d(o.X + scA * tblMargin, o.Y + scA * (chenar.Height - 4 * rowHeight), 0)
                    tabel1.SetSize(2, 3)
                    tabel1.SetRowHeight(rowHeight * scA)
                    tabel1.Cells.TextHeight = txtRowHeight * scA
                    tabel1.Columns(0).Width = 30 * scA
                    tabel1.Columns(1).Width = 50 * scA
                    tabel1.Columns(2).Width = (chenar.Width - 2 * tblMargin) * scA - tabel1.Columns(0).Width - tabel1.Columns(1).Width
                    tabel1.Cells(0, 0).TextString = "Nr. Cadastral"
                    tabel1.Cells(0, 1).TextString = "Suprafata Măsurată"
                    tabel1.Cells(0, 2).TextString = "Adresa Imobilului"
                    tabel1.Cells(1, 1).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.Area, 1).ToString & " mp"
                    If Anexa.Imobil.Adresa.adresaIntreaga IsNot Nothing Then tabel1.Cells(1, 2).TextString = Anexa.Imobil.Adresa.adresaIntreaga.ToString
                    acBlkTblRec.AppendEntity(tabel1)
                    acTrans.AddNewlyCreatedDBObject(tabel1, True)

                    Dim tabel2 As New Table
                    With tabel2
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabel1.Position.X, tabel1.Position.Y - tabel1.MinimumTableHeight, 0)
                        .SetSize(1, 4)
                        .SetRowHeight(rowHeight * scA)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Columns(0).Width = 50 * scA
                        .Columns(1).Width = 30 * scA
                        .Columns(2).Width = 30 * scA
                        .Columns(3).Width = (chenar.Width - 2 * tblMargin) * scA - .Columns(0).Width - .Columns(1).Width - .Columns(2).Width
                        .Cells(0, 0).TextString = "Carte Funciară Nr."
                        .Cells(0, 1).TextString = ""
                        .Cells(0, 2).TextString = "UAT"
                        .Cells(0, 3).TextString = Anexa.Imobil.UAT.Denumire
                    End With
                    acBlkTblRec.AppendEntity(tabel2)
                    acTrans.AddNewlyCreatedDBObject(tabel2, True)

                    Dim coordRect As New Polyline(4)
                    With coordRect
                        .AddVertexAt(0, bl, Nothing, Nothing, Nothing)
                        .AddVertexAt(1, br, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, tr, Nothing, Nothing, Nothing)
                        .AddVertexAt(3, tl, Nothing, Nothing, Nothing)
                        .Closed = True
                    End With
                    acBlkTblRec.AppendEntity(coordRect)
                    acTrans.AddNewlyCreatedDBObject(coordRect, True)

                    Dim lineBL As New Polyline(3)
                    With lineBL
                        .AddVertexAt(0, New Point2d(bl.X - 4 * scA, bl.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, bl, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(bl.X, bl.Y - 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineBL)
                    acTrans.AddNewlyCreatedDBObject(lineBL, True)
                    Dim NBL As New DBText
                    With NBL
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(bl.X, bl.Y, 0)
                        .TextString = "N=" & CInt(bl.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NBL)
                    acTrans.AddNewlyCreatedDBObject(NBL, True)
                    Dim EBL As New DBText
                    With EBL
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(bl.X, bl.Y, 0)
                        .TextString = "E=" & CInt(bl.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(EBL)
                    acTrans.AddNewlyCreatedDBObject(EBL, True)

                    Dim lineBR As New Polyline(3)
                    With lineBR
                        .AddVertexAt(0, New Point2d(br.X + 4 * scA, br.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, br, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(br.X, br.Y - 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineBR)
                    acTrans.AddNewlyCreatedDBObject(lineBR, True)
                    Dim NBR As New DBText
                    With NBR
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(br.X, br.Y, 0)
                        .TextString = "N=" & CInt(br.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NBR)
                    acTrans.AddNewlyCreatedDBObject(NBR, True)
                    Dim EBR As New DBText
                    With EBR
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(br.X, br.Y, 0)
                        .TextString = "E=" & CInt(br.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(EBR)
                    acTrans.AddNewlyCreatedDBObject(EBR, True)

                    Dim lineTR As New Polyline(3)
                    With lineTR
                        .AddVertexAt(0, New Point2d(tr.X + 4 * scA, tr.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, tr, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(tr.X, tr.Y + 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineTR)
                    acTrans.AddNewlyCreatedDBObject(lineTR, True)
                    Dim NTR As New DBText
                    With NTR
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tr.X, tr.Y, 0)
                        .TextString = "N=" & CInt(tr.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NTR)
                    acTrans.AddNewlyCreatedDBObject(NTR, True)
                    Dim ETR As New DBText
                    With ETR
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(tr.X, tr.Y, 0)
                        .TextString = "E=" & CInt(tr.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(ETR)
                    acTrans.AddNewlyCreatedDBObject(ETR, True)

                    Dim lineTL As New Polyline(3)
                    With lineTL
                        .AddVertexAt(0, New Point2d(tl.X - 4 * scA, tl.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, tl, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(tl.X, tl.Y + 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineTL)
                    acTrans.AddNewlyCreatedDBObject(lineTL, True)
                    Dim NTL As New DBText
                    With NTL
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tl.X, tl.Y, 0)
                        .TextString = "N=" & CInt(tl.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NTL)
                    acTrans.AddNewlyCreatedDBObject(NTL, True)
                    Dim ETL As New DBText
                    With ETL
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tl.X, tl.Y, 0)
                        .TextString = "E=" & CInt(tl.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(ETL)
                    acTrans.AddNewlyCreatedDBObject(ETL, True)

                    '' '' '' '' '' ''For i = 1 To Anexa.Imobil.Constructii.Count
                    '' '' '' '' '' ''    Dim txtC As New DBText
                    '' '' '' '' '' ''    'Dim centroid As New Point3d((Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MaxPoint.X + Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MinPoint.X) / 2, _
                    '' '' '' '' '' ''    '        (Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MaxPoint.Y + Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MinPoint.Y) / 2, _
                    '' '' '' '' '' ''    '        0)
                    '' '' '' '' '' ''    With txtC
                    '' '' '' '' '' ''        .SetDatabaseDefaults()
                    '' '' '' '' '' ''        .HorizontalMode = TextHorizontalMode.TextCenter
                    '' '' '' '' '' ''        .VerticalMode = TextVerticalMode.TextVerticalMid
                    '' '' '' '' '' ''        .AlignmentPoint = centroid(Anexa.Imobil.Constructii(i - 1).Polyline.Geometry)
                    '' '' '' '' '' ''        .TextString = "C" & CInt(i).ToString
                    '' '' '' '' '' ''        .Height = 2 * scA
                    '' '' '' '' '' ''        .AdjustAlignment(acCurDb)
                    '' '' '' '' '' ''    End With
                    '' '' '' '' '' ''    acBlkTblRec.AppendEntity(txtC)
                    '' '' '' '' '' ''    acTrans.AddNewlyCreatedDBObject(txtC, True)
                    '' '' '' '' '' ''Next
                    numberPolyline(Anexa.Imobil.Polyline.Geometry, scA, acCurDb, acTrans, acBlkTblRec)
                    dimPolyline(Anexa.Imobil.Polyline.Geometry, scA, acCurDb, acTrans, acBlkTblRec)
                    veciniPolyline(Anexa.Imobil.Polyline.Geometry, scA, Anexa.Imobil.Vecini, acCurDb, acTrans, acBlkTblRec)

                    Dim titluri As New DBText
                    With titluri
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextCenter
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(o.X + chenar.Width / 2 * scA, o.Y + (jos - 1 * rowHeight) * scA, 0)
                        .TextString = "Tabel de mișcare parcelară pentru " & tipDoc
                        .Height = 4 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(titluri)
                    acTrans.AddNewlyCreatedDBObject(titluri, True)

                    Dim tabelImobile As New Table
                    With tabelImobile
                        .SetDatabaseDefaults()
                        .SetSize(3 + Anexa.Imobile.Count, 3)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Rows(0).IsMergeAllEnabled = True
                        .SetRowHeight(rowHeight * scA)
                        If Dezlipire Then
                            .Cells(0, 0).TextString = "Situația viitoare"
                            .Position = New Point3d(tabel1.Position.X + 80 * scA, titluri.Position.Y, 0)
                        Else
                            .Cells(0, 0).TextString = "Situația actuală"
                            .Position = New Point3d(tabel1.Position.X, titluri.Position.Y, 0)
                        End If
                        .Columns(0).Width = 20 * scA
                        .Columns(1).Width = 20 * scA
                        .Columns(2).Width = 40 * scA
                        .Cells(1, 0).TextString = "Nr. cad"
                        .Cells(1, 1).TextString = "Suprafața (mp)"
                        .Cells(1, 2).TextString = "Descrierea imobilului"
                        Dim totalSuprafata As Decimal
                        totalSuprafata = 0
                        Try
                            For i = 2 To .Rows.Count - 2
                                .Cells(i, 0).TextString = Anexa.Imobile(i - 2).NrCadastral.ToString
                                .Cells(i, 1).TextString = Math.Round(Anexa.Imobile(i - 2).Polyline.Geometry.Area, 0).ToString
                                totalSuprafata += Anexa.Imobile(i - 2).Polyline.Geometry.Area
                                .Cells(i, 2).TextString = Anexa.Imobile(i - 2).Descriere
                            Next
                            .Cells(.Rows.Count - 1, 0).TextString = "Total"
                            .Cells(.Rows.Count - 1, 1).TextString = Math.Round(totalSuprafata, 0).ToString
                        Catch ex As Exception
                            MsgBox("TabelImobile: " + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelImobile)
                    acTrans.AddNewlyCreatedDBObject(tabelImobile, True)

                    Dim tabelImobil As New Table
                    With tabelImobil
                        .SetDatabaseDefaults()

                        .SetSize(3 + Anexa.Imobile.Count, 3)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Rows(0).IsMergeAllEnabled = True
                        .SetRowHeight(rowHeight * scA)
                        If Not Dezlipire Then
                            .Cells(0, 0).TextString = "Situația viitoare"
                            .Position = New Point3d(tabel1.Position.X + 80 * scA, titluri.Position.Y, 0)
                        Else
                            .Cells(0, 0).TextString = "Situația actuală"
                            .Position = New Point3d(tabel1.Position.X, titluri.Position.Y, 0)
                        End If
                        .Columns(0).Width = 20 * scA
                        .Columns(1).Width = 20 * scA
                        .Columns(2).Width = 40 * scA
                        .Cells(1, 0).TextString = "Nr. cad."
                        .Cells(1, 1).TextString = "Suprafața (mp)"
                        .Cells(1, 2).TextString = "Descrierea imobilului"
                        Try
                            .Cells(2, 0).TextString = Anexa.Imobil.NrCadastral.ToString
                            .Cells(2, 1).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.Area, 0).ToString
                            .Cells(2, 2).TextString = Anexa.Imobil.Descriere
                            .Cells(.Rows.Count - 1, 0).TextString = "Total"
                            .Cells(.Rows.Count - 1, 1).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.Area, 0).ToString
                        Catch ex As Exception
                            MsgBox("TabelImobil: " + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelImobil)
                    acTrans.AddNewlyCreatedDBObject(tabelImobil, True)

                    Dim txtExecutant As New DBText
                    With txtExecutant
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextCenter
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(o.X + 40 * scA, o.Y + (tblMargin + 5) * scA, 0)
                        .TextString = "Executant"
                        .Height = txtRowHeight * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(txtExecutant)
                    acTrans.AddNewlyCreatedDBObject(txtExecutant, True)

                    Dim txtData As New DBText
                    With txtData
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextCenter
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(o.X + 40 * scA, o.Y + (tblMargin) * scA, 0)
                        .TextString = DataPAD.ToShortDateString
                        .Height = txtRowHeight * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(txtData)
                    acTrans.AddNewlyCreatedDBObject(txtData, True)
                    '' Save the new object to the database
                    acCurDb.Clayer = acLyrTbl(oldLayer.Name)
                    acTrans.Commit()
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.Data.ToString)
        End Try

    End Sub
    Public Function CanGenereazaPAD()
        Return True
    End Function


End Class
